<?php
class safeService extends commonService{
    private $d=null;
    private $table=null;
    public function __construct(){
        parent::__construct();
        $this->table=table('admin');
    }
    //检测表单项不为空
    public function check_blank1($data){
        if(trim($data['user_password'])=='' or trim($data['user_name'])==''){
            return false;
        }
        $this->d=$data;
        return true;
    }
    //检测表单项不为空
    public function check_blank2($data){
        if(trim($data['old_password'])=='' or trim($data['user_password'])==''){
            return false;
        }
        $this->d=$data;
        return true;
    }
    //返回用户id
    private function get_id(){
        return $_SESSION['MY']['ID'];
    }
    //确认旧密码
    public function is_correct($f){
        $id=$this->get_id();
        $query="select `user_password` from `{$this->table}` where `id`=:id limit 1";
        $r=$this->m->get_field($query,array('id'=>$id));
        return md5password($this->d[$f])==$r;
    }
    //更新密码
    public function update_pwd(){
        $id=$this->get_id();
        $pwd=md5password($this->d['user_password']);
        $query="update `{$this->table}` set `user_password`=:pwd where `id`=:id limit 1";
        return $this->m->query($query,array('pwd'=>$pwd,'id'=>$id));
    }
    //更新账号
    public function update_name(){
        $id=$this->get_id();
        $name=$this->d['user_name'];
        //如果账号已存在则返回假
        $query="select `id` from `{$this->table}` where `id`!=:id and `user_name`=:name limit 1";
        if($this->m->get_field($query,array('id'=>$id,'name'=>$name))){
            return false;
        }
        $query="update `{$this->table}` set `user_name`=:name where `id`=:id limit 1";
        return $this->m->query($query,array('id'=>$id,'name'=>$name));
    }
}